//
//  CHBVistorView.swift
//  weibo.chb
//
//  Created by 褚红彪 on 16/1/21.
//  Copyright © 2016年 chuhongbiao. All rights reserved.
//

import UIKit
import SnapKit
/*protocol CHBVistorViewDelegate: NSObjectProtocol{
    func registerButtonSelect()
    func loginButtonSelect()
}*/
class CHBVistorView: UIView {
    //weak var delegate : CHBVistorViewDelegate?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(codel:) has not been implemented")
        
    }
    func setVistor(imgName : String?,title : String?){
        if let img = imgName, let tle = title{
            circleView.hidden = true
            iconView.image = UIImage(named: img)
            messageLabel.text = tle
        }else{
            startAnim()
        }
    }
    func startAnim(){
        let anim = CABasicAnimation(keyPath:"transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    func setupUI(){
        backgroundColor = UIColor(white: 237 / 255, alpha: 1.0)
        addSubview(circleView)
        addSubview(maskBackGroundView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
       //给iconView添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        //给circle添加约束
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        //给消息标签添加通知
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.width.equalTo(224)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        //给注册按钮添加约束
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.leading.equalTo(messageLabel.snp_leading)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        //给登陆按钮添加约束
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        //给半透明的背景图添加约束
        maskBackGroundView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.bottom.equalTo(registerButton.snp_bottom)
            make.width.equalTo(self)
        }
    }
    func yueshuDemo(){
        //把Autoresizing关掉才能使用Autoreslayout
        //给iconView添加约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        //给circle添加约束
        circleView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        //给消息标签添加通知
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 180))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        //给注册按钮添加约束
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 80))
        //给登陆按钮添加约束
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 80))
        //给半透明的背景图添加约束
        maskBackGroundView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskBackGroundView]-0-|", options: [], metrics: nil, views: ["maskBackGroundView" : maskBackGroundView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskBackGroundView]-(offset)-[registerButton]", options: [], metrics: ["offset" : 0], views: ["maskBackGroundView" : maskBackGroundView,"registerButton" : registerButton]))
    }
    private lazy var iconView : UIImageView = {
       let img = UIImage(named: "visitordiscover_feed_image_house")
       let imgView = UIImageView(image: img)
       return imgView
    }()
    private lazy var circleView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var messageLabel : UILabel = {
       let label = UILabel()
        label.text = "关注一些人，在这里会发现惊喜关注一些人，在这里会发现惊喜"
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
        return label
    }()
    lazy var registerButton : UIButton = {
        let btn = UIButton()
       // btn.addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        return btn
    }()
    /*@objc private func registerBtnClick(){
        delegate?.registerButtonSelect()
    }*/
    lazy var loginButton : UIButton = {
        let btn = UIButton()
       // btn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        return btn
        }()
    /*@objc private func loginBtnClick(){
        delegate?.loginButtonSelect()
    }*/
    private lazy var maskBackGroundView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
}
